package com.wenhui.schoolyardinfo.login.model;

import android.content.Context;

import com.wenhui.schoolyardinfo.login.callback.OnLoginListener;
import com.wenhui.schoolyardinfo.login.model.base.ILoginModel;
import com.wenhui.schoolyardinfo.manager.Okhttp3Manager;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/7/9 17:03
 * introduce :
 */

public class LoginModelImpl implements ILoginModel {

    private Context context;
    private Okhttp3Manager okhttp3Manager;

    public LoginModelImpl(Context context) {
        this.context = context;
    }

    @Override
    public void login(Map<String, String> bodyParams, final OnLoginListener onLoginListener) {
        okhttp3Manager = Okhttp3Manager.getInstance(context);
        okhttp3Manager.postAsyncFormOkhttp3("",
                null,
                bodyParams,
                new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        onLoginListener.onLoginError(e.toString());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        onLoginListener.onLoginSuccess(response.body().string());
                    }
                });
    }

    @Override
    public void register(Map<String, String> bodyParams, final OnLoginListener onLoginListener) {
        okhttp3Manager = Okhttp3Manager.getInstance(context);
        okhttp3Manager.postAsyncFormOkhttp3("",
                null,
                bodyParams,
                new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        onLoginListener.onLoginError(e.toString());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        onLoginListener.onLoginSuccess(response.body().string());
                    }
                });
    }

    @Override
    public void changePassword(Map<String, String> bodyParams, final OnLoginListener onLoginListener) {
        okhttp3Manager = Okhttp3Manager.getInstance(context);
        okhttp3Manager.postAsyncFormOkhttp3("",
                null,
                bodyParams,
                new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        onLoginListener.onLoginError(e.toString());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        onLoginListener.onLoginSuccess(response.body().string());
                    }
                });
    }

}
